<?php

/*

Inbox (mail.php)

*/

$page_title = "Mail Inbox";
$rank_check = 3;
include "header.inc.php";

print "$openHTML";

if (!$start) { $start = "0"; }

$numUnread = mysql_num_rows(mysql_query("SELECT id FROM mail_topics2 WHERE to_user = '$userid' AND status = '0' AND game = '$game'"));

$lastid = fetch("SELECT id FROM mail_topics2 WHERE to_user = '$userid' AND game = '$game' ORDER BY timestamp DESC LIMIT 1");
$findMail = mysql_query("SELECT * FROM mail_topics2 WHERE to_user = '$userid' AND game = '$game' ORDER BY timestamp DESC LIMIT $start,10");
while ($getMail = mysql_fetch_array($findMail))
{
	$cellcolor = alternate($cellcolor,"#FFFFFF","$reallyLight");
	$mailID = $getMail[id];
	$getSender = fetch("SELECT username,display_name,premium,display_prem FROM members2 WHERE id = '$getMail[from_user]' AND game = '$game'");
	if (($getSender[premium] == 1) AND ($getSender[display_prem]))
	{
		$getSender[display_name] = $getSender[display_prem];
	}
	$sent_date[$mailID] = date("H:i, M j",$getMail[timestamp]);
	if ($getMail[status] == 0) { $status[$mailID] = "Unread"; $bold[$mailID] = "<b>"; $endBold[$mailID] = "</b>"; }
	if ($getMail[status] == 1) { $status[$mailID] = "Read"; }
	if ($getMail[status] == 2) { $status[$mailID] = "Replied"; }

	$inbox .= "

<TR bgcolor=$cellcolor>
<TD WIDTH=40> <CENTER><INPUT TYPE=checkbox NAME=mailAction[$mailID] VALUE=1></CENTER> </TD>
<TD><p>$bold[$mailID]<a href=mail_view.php?game=$game&id=$mailID>$getMail[subject]</a>$endBold[$mailID]<Br><font size=-1>Sent: $bold[$mailID]$sent_date[$mailID]$endBold[$mailID]</font></P></TD>
<TD WIDTH=110> <CENTER><a href=user_profile.php?game=$game&user=$getSender[username]>$getSender[display_name]</a></CENTER> </TD>
<TD WIDTH=90> <CENTER><FONT COLOR=\"#0000FF\"><B>$bold[$mailID]$status[$mailID]$endBold[$mailID]</B></FONT><BR>
<FONT COLOR=\"#FF0000\"><B><a href=mail_delete.pro.php?game=$game&id=$mailID>Delete</a></B></FONT></CENTER>
</TD></TR>

	";
}

$numMessages = mysql_num_rows(mysql_query("SELECT id FROM mail_topics2 WHERE to_user = '$userid' AND game = '$game'"));

for ($x = 0; $x <= $numMessages;)
{
	if ($x == 0) { $fullBar .= "<font color=#FF0000>"; }
	if ($x != $numMessages) { $fullBar .= "|"; }
	if ($x == $numMessages) { $fullBar .= "</font>"; }
	if ($getInfo[premium] != 1) { $x++; }
	if ($getInfo[premium] == 1) { $x += 5; }
}

if ($getInfo[premium] != 1) { $max = 50; }
if ($getInfo[premium] == 1) { $max = 250; }

for ($y = $numMessages; $y <= $max;)
{
	if ($y == $numMessages) { $fullBar .= "<font color=#0000FF>"; }
	if ($y != $max) { $fullBar .= "|"; }
	if ($y == $max) { $fullBar .= "</font>"; }
	if ($getInfo[premium] != 1) { $y++; }
	if ($getInfo[premium] == 1) { $y += 5; }
}

if ($numMessages == 0) { $numMessages = 1; }
$num_pages = ceil($numMessages / 10);

$x = 0;
$list_pages = "<p>Pages: ";

while ($x < $num_pages)
{
	if ($x == $num_pages)
	{
		$end[$x] = "</p>";
	}
	if ($x < $num_pages)
	{
		$end[$x] = "&nbsp;&nbsp;&nbsp;";
	}
	$start = $x * 10;
	$start2 = $start + 10;
	$list_pages .= "[<a href=$base_url/mail.php?game=$game&start=$start>$start - $start2</a>]$end[$x]";
	$x++;
}

$mailTable = " <p align=center><TABLE BGCOLOR=\"$reallyLight\" CELLSPACING=0 CELLPADDING=0 WIDTH=400
HEIGHT=55> <TR> <TD> <CENTER><font size=-1> $fullBar<BR> <A
HREF=\"mail_send.php?game=$game\"><FONT SIZE=\"-1\">Send Mail</FONT></A><FONT SIZE=\"-1\"> |
</FONT><A HREF=\"buds.php?game=$game\"><FONT SIZE=\"-1\">Buddies</FONT></A><FONT SIZE=\"-1\"> |
</FONT><A HREF=\"mail_delete.pro.php?game=$game&all=true&lastid=$lastid[id]\" onclick=\"if (
!confirm ('WARNING: By clicking ok ALL of your messages will be deleted permanatly!') ) {
return false; }\"><FONT SIZE=\"-1\">Clear Inbox</FONT></A><FONT SIZE=\"-1\"><BR>
<I>$numMessages/$max Messages, $numUnread Unread Messages</I></FONT></CENTER> </TD> </TR>
</TABLE> </p>";


print "

$mailTable

<form action=mail_delete.pro.php?game=$game method=post>

   <CENTER><BR>
<TABLE BGCOLOR=\"#000000\" CELLSPACING=0 CELLPADDING=0 WIDTH=475>
   <TR>
      <TD BGCOLOR=\"$topAndBottomBG\">
         <CENTER><IMG SRC=\"omegapets/images/newdesign/mail_inbox.gif\" WIDTH=475 HEIGHT=14 ALIGN=bottom></CENTER>
      </TD>
   </TR>
</TABLE>
 <TABLE BGCOLOR=\"#000000\" CELLSPACING=1 CELLPADDING=0 WIDTH=475>

	$inbox

   <tr bgcolor=#FFFFFF>
   <td colspan=4>
   $list_pages
   </td></tr>
</TABLE>
</CENTER>

$mailTable

<P align=center><input type=submit name=deleteall value=\"Delete Checked\"></P></FORM>

";

print "$closeHTML";
?>